﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.CustomerService.Controllers
{
    public class ProductModelController : Controller
    {
        // GET: Model
        [HttpPost]
        public JsonResult Index(ProductModelsModel model)
        {
            var total = 0;
            var list = ServiceFactory.ProductModelsServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                BrandName = o.ProductBrands != null ? o.ProductBrands.BrandName : string.Empty,
                o.ID,
                o.ModelName,
                o.CreateBy,
                CreateTime = o.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                o.ImgUrl,
                o.Remark,
                ScreenTypeName = EnumHelper.Instance.GetDescription(typeof(ScreenTypeEnum), o.ScreenType),
                o.IsAvailable,
                LastModifyTime = o.LastModifyTime.ToString("yyyy-MM-dd HH:mm:ss"),
                o.LastModifyBy,
                o.Sort,
                o.ModelCode
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetModelList(int brandID)
        {
            var modelList = ServiceFactory.ProductModelsServiceInstance.GetProductModel(new ProductModelsModel { BrandId = brandID }).Select(
                o => new
                {
                    o.BrandId,
                    o.ID,
                    o.ModelCode,
                    o.ModelName,
                    o.ScreenType
                }).ToList();

            return Json(new
            {
                success = true,
                ModelList = modelList
            }, JsonRequestBehavior.AllowGet);
        }
    }
}